﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DirectX_CS
{
	public partial class OutputForm : Form, INamed
	{
		//public System.Threading.Timer mHideFormTimer = null;
		protected int sleepTime;
		protected Form mWindow = null;
		protected bool mCancelHide = false;
		protected static OutputForm primaryOutputForm = null;

		protected string shortName = "Output";
		public string ShortName
		{
			get { return shortName; }
			set { shortName = value; }
		}
		private int shortNumber = 0;

		public int ShortNumber
		{
			get { return shortNumber; }
			set { shortNumber = value; }
		}
		[PGBrowsable]
		public string ObjectName
		{
			get { return shortName + " " + shortNumber; }
		}
		public static OutputForm PrimaryOutputForm
		{
			get { return primaryOutputForm; }
			set { primaryOutputForm = value; }
		}

		public OutputForm(Form DirectXWindow)
		{
			primaryOutputForm = this;
			InitializeComponent();
			mWindow = DirectXWindow;
		}

		public void AppendLine(string outputMessage)
		{
			//append to top
			txtOutput.Text = outputMessage + "\r\n" + txtOutput.Text;
		}
		public void OutputThenGoAway(string outputMessage)
		{
			OutputThenGoAway(outputMessage, 1000);
		}

		public void OutputThenGoAway(string outputMessage, int msecondsUntilGoesAway)
		{
			mCancelHide = false;
			sleepTime = msecondsUntilGoesAway;
			AppendLine(outputMessage);
			backgroundWorker1.CancelAsync();
			if (!backgroundWorker1.IsBusy)
				backgroundWorker1.RunWorkerAsync();

			this.ShowAFresh();
		}

		public void ShowAFresh()
		{
			this.TopMost = true;
			if (!this.Visible)
			{
				if (this.WindowState == FormWindowState.Minimized)
					this.WindowState = FormWindowState.Normal;
				this.Show();
				this.Activate();

			}
			else
			{
				//this.Hide();
				this.WindowState = FormWindowState.Normal;
				this.Show();
				this.Activate();
			}

			
		}

		private void OutputForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				this.Hide();
				e.Cancel = true;
			}
		}

		private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
		{
			Thread.Sleep(this.sleepTime);
		}

		private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (!mCancelHide)
			{
				this.WindowState = FormWindowState.Minimized;
				if (mWindow != null)
					mWindow.Show();
			}
			backgroundWorker1.CancelAsync();
		}

		private void OutputForm_Load(object sender, EventArgs e)
		{
			this.Location = new Point(mWindow.Right - this.Width - 10, mWindow.Bottom - this.Height - 10);
		}

		private void OutputForm_Click(object sender, EventArgs e)
		{
			mCancelHide = true;
		}

		private void txtOutput_Click(object sender, EventArgs e)
		{
			mCancelHide = true;
		}

		private void OutputForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (!(e.Control || e.Shift || e.Alt))  //maybe they are trying to copy something.
			{
				this.WindowState = FormWindowState.Minimized;
			}
		}

	}
}
